package com.pp.controller;


import com.pp.bean.GovStory;
import com.pp.bean.GovStoryInfo;
import com.pp.bean.GovStoryInfoImage;
import com.pp.service.GovStoryService;
import com.pp.util.Respons.ResponseUtil;
import com.pp.util.upload.UploadFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 品牌故事类型 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-04-02
 */
@Controller
@RequestMapping("/govStory")
@Api(value = "品牌故事类型Controller",tags = {"品牌故事类型类型类"})
public class GovStoryController {


    @Autowired
    GovStoryService govStoryService;

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增品牌故事类型")
    public Map insert(
            @RequestParam("lang") Integer lang,
            @RequestParam("width") Integer width,
            @RequestParam("height") Integer height,
            @RequestParam("file") MultipartFile file
            ){
        GovStory govStory = new GovStory();
        govStory.setWidth(width);
        govStory.setLang(lang);
        govStory.setHeight(height);


        try {
            String url = UploadFile.fileUpload(file);
            govStory.setImgUrl(url);
            System.out.println("返回保存文件的路径:"+url);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        if(govStoryService.save(govStory)){
            /*GovStoryInfo govStoryInfo = new GovStoryInfo();
            govStoryInfo.setRelactionId(govStory.getId());
            System.out.println("123:"+govStoryInfo);*/
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }



    @RequestMapping(value = "deleteById",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除品牌故事类型")
    public Map deleteById(@RequestBody GovStory govStory){
        if(govStoryService.removeById(govStory)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }


    @RequestMapping(value = "/updateById",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改品牌故事")
    public Map updateById(@RequestBody GovStory govStory){
        if(govStoryService.updateById(govStory)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }

    @RequestMapping(value = "select",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询品牌故事")
    public List<GovStory> select(){
        return govStoryService.list();
    }

}
